package top.juntower.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.juntower.pojo.Comment;
import top.juntower.pojo.GameTypeInformation;
import top.juntower.pojo.ReplyComments;
import top.juntower.pojo.vo.CommentVo;
import top.juntower.pojo.vo.GameComment;
import top.juntower.pojo.vo.ReplyCommentsVo;
import top.juntower.service.impl.CommentServiceImpl;
import top.juntower.utils.JsonResult;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@Api(tags = "评论管理")
@RestController
@RequestMapping("comment")
public class CommentController {

    @Autowired
    CommentServiceImpl commentService;

    //添加用户评论
    @ApiOperation(value = "添加用户评论",response = GameTypeInformation.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "giId", value = "游戏Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "uId", value = "用户Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "cContent", value = "评论内容", dataType = "String", required = true),
            @ApiImplicitParam(name = "cScore", value = "用户评分", dataType = "Integer", required = true),
    })
    @GetMapping("/addComment")
    public JsonResult addComment(Integer giId, Integer uId,String cContent,Integer cScore){
        if (giId == null || uId == null || cContent == null || cScore == null){
            return JsonResult.error(JsonResult.NO);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("giId",giId);
        map.put("uId",uId);
        map.put("cContent",cContent);
        map.put("cScore",cScore);
        map.put("cStatetime",new Date());
        int i = commentService.addComment(map);
        if (i<0){
            return  JsonResult.error(JsonResult.INSERT_MISS);
        }
        Double cscore = commentService.cscore(giId);
        if (cscore == null){
            return JsonResult.error(JsonResult.MISS);
        }
        DecimalFormat df = new DecimalFormat("#.0");
        int update = commentService.updateCscore(giId,Double.parseDouble(df.format(cscore)));
        if (update > 0){
            return  JsonResult.ok(JsonResult.UPDATE_SUCCESS);
        }
        return  JsonResult.error(JsonResult.UPDATE_MISS);
    }

    //查询用户是否评论
    @ApiOperation(value = "查询用户是否评论",response = GameTypeInformation.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "giId", value = "游戏Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "uId", value = "用户Id", dataType = "Integer", required = true),
    })
    @GetMapping("/isComment")
    public JsonResult isComment(Integer giId, Integer uId){
        if (giId == null || uId == null ){
            return JsonResult.error(JsonResult.NO);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("giId",giId);
        map.put("uId",uId);
        String comment = commentService.isComment(map);
        if (comment == null){
            return JsonResult.ok(false);
        }
        return  JsonResult.ok(true);
    }

    //查询默认评论
    @ApiOperation(value = "查询默认评论",response = GameTypeInformation.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "giId", value = "游戏Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "uId", value = "用户Id（用户Id可有可无）", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "pageNum", value = "分页页码", dataType = "Integer", required = true),
    })
    @GetMapping("/commentNew")
    public JsonResult commentNew(Integer giId,Integer uId,Integer pageNum){
        if (giId == null || pageNum == null){
            return JsonResult.error(JsonResult.NO);
        }
        if (uId ==  null){
            PageHelper.startPage(pageNum,10);
            List<Comment> comments = commentService.iscommentNew(giId, pageNum, 10);
            PageInfo<Comment> commentPageInfo = new PageInfo<>(comments);
            return JsonResult.ok(commentPageInfo);
        }

        PageHelper.startPage(pageNum,10);
        List<Comment> comments = commentService.commentNew(giId,uId,pageNum,10);
        PageInfo<Comment> commentPageInfo = new PageInfo<>(comments);
        if (commentPageInfo == null){
            return JsonResult.error(JsonResult.MISS);
        }
        return  JsonResult.ok(commentPageInfo);
    }


    //查询最热评论
    @ApiOperation(value = "查询最热评论",response = GameTypeInformation.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "giId", value = "游戏Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "uId", value = "用户Id(用户Id可有可无)", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "pageNum", value = "分页页码", dataType = "Integer", required = true),
    })
    @GetMapping("/commentHot")
    public JsonResult commentHot(Integer giId,Integer uId,Integer pageNum){
        if (giId == null || pageNum == null ){
            return JsonResult.error(JsonResult.NO);
        }
        if (uId ==  null){
            PageHelper.startPage(pageNum,10);
            List<Comment> comments = commentService.iscommentHot(giId, pageNum, 10);
            PageInfo<Comment> commentPageInfo = new PageInfo<>(comments);
            return JsonResult.ok(commentPageInfo);
        }
        PageHelper.startPage(pageNum,10);
        List<Comment> comments = commentService.commentHot(giId,uId,pageNum,10);
        PageInfo<Comment> commentPageInfo = new PageInfo<>(comments);
        if (commentPageInfo == null){
            return JsonResult.error(JsonResult.MISS);
        }
        return  JsonResult.ok(commentPageInfo);
    }

    //添加点赞
    @ApiOperation(value = "添加点赞",response = GameTypeInformation.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "giId", value = "游戏Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "uId", value = "用户Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "cId", value = "评论Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "isState", value = "点赞状态", dataType = "boolean", required = true)
    })
    @GetMapping("/fabulous")
    public JsonResult fabulous(Integer giId,Integer uId,Integer cId,boolean isState){
        if (giId == null || uId == null || cId == null ){
            return JsonResult.error(JsonResult.NO);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("giId",giId);
        map.put("uId",uId);
        map.put("cId",cId);
        map.put("isState",isState);
        map.put("cmTime",new Date());
        String commentm = commentService.isCommentm(map);
        if(commentm==null){
            commentService.insertCommentm(map);
            commentService.updateComment(map);
            return  JsonResult.ok(JsonResult.UPDATE_SUCCESS);
        }else {
            commentService.updateCmState(map);
            if (isState){
                commentService.updateComment(map);
                return  JsonResult.ok(JsonResult.UPDATE_SUCCESS);
            }else {
                commentService.updateComment1(map);
                return  JsonResult.ok(JsonResult.UPDATE_SUCCESS);
            }
        }
    }

    //回复评论
    @ApiOperation(value = "回复评论",response = GameTypeInformation.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "giId", value = "游戏Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "uId", value = "用户Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "cId", value = "评论Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "grContent", value = "回复评论内容", dataType = "string", required = true),
            @ApiImplicitParam(name = "grTouid", value = "回复用户Id", dataType = "string", required = true),
    })
    @GetMapping("/addGamereview")
    public JsonResult addGamereview(Integer giId, Integer uId,String grContent,Integer cId,Integer grTouid){
        if (giId == null || uId == null || grContent == null || cId == null ){
            return JsonResult.error(JsonResult.NO);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("giId",giId);
        map.put("uId",uId);
        map.put("grContent",grContent);
        map.put("cId",cId);
        map.put("grTouid",grTouid);
        map.put("grStatetime",new Date());
        int i = commentService.addGamereview(map);
        if (i>0){
            return JsonResult.ok(JsonResult.INSERT_SUCCESS);
        }
        return  JsonResult.error(JsonResult.INSERT_MISS);
    }

    //查询回复信息
    @ApiOperation(value = "查询回复信息",response = GameTypeInformation.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cId", value = "评论管理Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "pageNum", value = "分页页码", dataType = "Integer", required = true),
    })
    @GetMapping("/replyComments")
    public JsonResult replyComments(Integer cId,Integer pageNum){
        if (cId == null || pageNum == null){
            return JsonResult.error(JsonResult.NO);
        }
        PageHelper.startPage(pageNum,10);
        List<ReplyComments> replyComments = commentService.replyComments(cId, pageNum, 10);
        PageInfo<ReplyComments> commentPageInfo = new PageInfo<ReplyComments>(replyComments);
        if (commentPageInfo == null){
            return JsonResult.error(JsonResult.MISS);
        }
        return  JsonResult.ok(commentPageInfo);
    }

    //我的评价
    @ApiOperation(value = "我的评价",response = GameTypeInformation.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uId", value = "用户Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "pageNum", value = "分页页码", dataType = "Integer", required = true),
    })
    @GetMapping("/MyComments")
    public JsonResult MyComments(Integer uId,Integer pageNum){
        if (pageNum == null || uId == null){
            return JsonResult.error(JsonResult.NO);
        }
        PageHelper.startPage(pageNum,10);
        List<CommentVo> commentVos = commentService.MyComments(uId, pageNum, 10);
        PageInfo<CommentVo> commentPageInfo = new PageInfo<CommentVo>(commentVos);
        if (commentPageInfo == null){
            return JsonResult.error(JsonResult.MISS);
        }
        return  JsonResult.ok(commentPageInfo);
    }

    //我的信息
    @ApiOperation(value = "我的信息",response = GameTypeInformation.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uId", value = "用户Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "pageNum", value = "分页页码", dataType = "Integer", required = true),
    })
    @GetMapping("/myReplyComments")
    public JsonResult myReplyComments(Integer uId,Integer pageNum){
        if (pageNum == null || uId == null){
            return JsonResult.error(JsonResult.NO);
        }
        PageHelper.startPage(pageNum,10);
        List<ReplyCommentsVo> replyCommentsVos = commentService.myReplyComments(uId, pageNum, 10);
        PageInfo<ReplyCommentsVo> commentPageInfo = new PageInfo<ReplyCommentsVo>(replyCommentsVos);
        if (commentPageInfo == null){
            return JsonResult.error(JsonResult.MISS);
        }
        return  JsonResult.ok(commentPageInfo);
    }

    //游戏内我的评价
    @ApiOperation(value = "游戏内我的评价",response = GameTypeInformation.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uId", value = "用户Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "giId", value = "游戏Id", dataType = "Integer", required = true),
    })
    @GetMapping("/myGameComment")
    public JsonResult myGameComment(Integer uId,Integer giId){
        if (giId == null || uId == null){
            return JsonResult.error(JsonResult.NO);
        }
        GameComment gameComment = commentService.myGameComment(giId, uId);
        if (gameComment == null){
            return JsonResult.error(JsonResult.MISS);
        }
        return  JsonResult.ok(gameComment);
    }

    //删除评论
    @ApiOperation(value = "删除评论",response = GameTypeInformation.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cId", value = "评论Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "giId", value = "游戏Id", dataType = "Integer", required = true),
    })
    @GetMapping("/delComment")
    public JsonResult delComment(Integer cId,Integer giId){
        if (cId == null || giId == null){
            return JsonResult.error(JsonResult.NO);
        }
        int i = commentService.delComment(cId);
        if (i>0){
            Double cscore = commentService.cscore(giId);
            if (cscore == null){
                return JsonResult.error(JsonResult.MISS);
            }
            DecimalFormat df = new DecimalFormat("#.0");
            commentService.updateCscore(giId,Double.parseDouble(df.format(cscore)));
            int i1 = commentService.delCommentm(cId);
            return JsonResult.ok(JsonResult.DEL_SUCCESS);
        }
        return  JsonResult.ok(JsonResult.DEL_MISS);
    }

    //修改评论
    @ApiOperation(value = "修改评论",response = GameTypeInformation.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cId", value = "评论Id", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "cScore", value = "评论评分", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "cContent", value = "评论内容", dataType = "string", required = true),
            @ApiImplicitParam(name = "giId", value = "游戏Id", dataType = "Integer", required = true),
    })
    @GetMapping("/updComment")
    public JsonResult updComment(Integer cId,Integer cScore,String cContent,Integer giId ){
        if (cId == null || cScore == null || cContent == null || giId == null){
            return JsonResult.error(JsonResult.NO);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("cId",cId);
        map.put("cScore",cScore);
        map.put("cContent",cContent);
        int i = commentService.updComment(map);
        if (i>0){
            Double cscore = commentService.cscore(giId);
            if (cscore == null){
                return JsonResult.error(JsonResult.MISS);
            }
            DecimalFormat df = new DecimalFormat("#.0");
            int update = commentService.updateCscore(giId,Double.parseDouble(df.format(cscore)));
            if (update > 0){
                return  JsonResult.ok(JsonResult.UPDATE_SUCCESS);
            }
            return  JsonResult.error(JsonResult.UPDATE_MISS);
        }
        return  JsonResult.ok(JsonResult.UPDATE_MISS);
    }
}
